package swingUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import com.trsjh.db.controller.Home;
import com.trsjh.db.domains.GoodsType;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.table.DefaultTableModel;


public class GoodTypeUI extends JPanel implements MyUI {

	private BindingGroup m_bindingGroup;
	private com.trsjh.db.domains.GoodsType goodsType = new com.trsjh.db.domains.GoodsType();
	private JTextPane descriptionJTextPane;
	private JTextArea nameJTextArea;
	private JTable table;

	public GoodTypeUI(com.trsjh.db.domains.GoodsType newGoodsType) {
		this();
		setGoodsType(newGoodsType);
	}

	public GoodTypeUI() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 0.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4 };
		setLayout(gridBagLayout);

		JLabel descriptionLabel = new JLabel("Description:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		add(descriptionLabel, labelGbc_0);

		descriptionJTextPane = new JTextPane();
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.gridwidth = 2;
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		add(descriptionJTextPane, componentGbc_0);

		JLabel nameLabel = new JLabel("Name:");
		GridBagConstraints labelGbc_1 = new GridBagConstraints();
		labelGbc_1.insets = new Insets(5, 5, 5, 5);
		labelGbc_1.gridx = 0;
		labelGbc_1.gridy = 1;
		add(nameLabel, labelGbc_1);

		nameJTextArea = new JTextArea();
		GridBagConstraints componentGbc_1 = new GridBagConstraints();
		componentGbc_1.gridwidth = 2;
		componentGbc_1.insets = new Insets(5, 0, 5, 0);
		componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_1.gridx = 1;
		componentGbc_1.gridy = 1;
		add(nameJTextArea, componentGbc_1);
		
		JButton backButton = new JButton("\u64A4\u9500");
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nameJTextArea.setText("");
				descriptionJTextPane.setText("");
			}
		});
		GridBagConstraints gbc_backButton = new GridBagConstraints();
		gbc_backButton.insets = new Insets(0, 0, 5, 5);
		gbc_backButton.gridx = 0;
		gbc_backButton.gridy = 2;
		add(backButton, gbc_backButton);
		
		JButton saveButton = new JButton("\u6DFB\u52A0");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String description =descriptionJTextPane.getText();
				String name=nameJTextArea.getText();
				GoodsType goodsType=new GoodsType();
				goodsType.setDescription(description);
				goodsType.setName(name);
				Home home=new Home();
				home.beginTransaction();
				home.persist(goodsType);
				home.commit();
				list();
				home.closeEntityManager();
				
			}
		});
		GridBagConstraints gbc_saveButton = new GridBagConstraints();
		gbc_saveButton.insets = new Insets(0, 0, 5, 5);
		gbc_saveButton.gridx = 1;
		gbc_saveButton.gridy = 2;
		add(saveButton, gbc_saveButton);
		
		JButton searchButton = new JButton("\u67E5\u8BE2\u5168\u90E8");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list();
			}
		});
		GridBagConstraints gbc_searchButton = new GridBagConstraints();
		gbc_searchButton.insets = new Insets(0, 0, 5, 0);
		gbc_searchButton.gridx = 2;
		gbc_searchButton.gridy = 2;
		add(searchButton, gbc_searchButton);
		
		JLabel label = new JLabel("\u73B0\u6709\u7C7B\u522B");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 3;
		add(label, gbc_label);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 4;
		add(scrollPane, gbc_scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u5E8F\u53F7", "\u540D\u5B57", "\u63CF\u8FF0"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(2).setMaxWidth(2147483645);
		table.setDragEnabled(true);
		scrollPane.setViewportView(table);

		if (goodsType != null) {
			m_bindingGroup = initDataBindings();
		}
	}

	protected BindingGroup initDataBindings() {
		BeanProperty<com.trsjh.db.domains.GoodsType, java.lang.String> descriptionProperty = BeanProperty
				.create("description");
		BeanProperty<javax.swing.JTextPane, java.lang.String> textProperty = BeanProperty
				.create("text");
		AutoBinding<com.trsjh.db.domains.GoodsType, java.lang.String, javax.swing.JTextPane, java.lang.String> autoBinding = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, goodsType,
						descriptionProperty, descriptionJTextPane, textProperty);
		autoBinding.bind();
		//
		BeanProperty<com.trsjh.db.domains.GoodsType, java.lang.String> nameProperty = BeanProperty
				.create("name");
		BeanProperty<javax.swing.JTextArea, java.lang.String> textProperty_1 = BeanProperty
				.create("text");
		AutoBinding<com.trsjh.db.domains.GoodsType, java.lang.String, javax.swing.JTextArea, java.lang.String> autoBinding_1 = Bindings
				.createAutoBinding(AutoBinding.UpdateStrategy.READ, goodsType,
						nameProperty, nameJTextArea, textProperty_1);
		autoBinding_1.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_1);
		//
		return bindingGroup;
	}

	public com.trsjh.db.domains.GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(com.trsjh.db.domains.GoodsType newGoodsType) {
		setGoodsType(newGoodsType, true);
	}

	public void setGoodsType(com.trsjh.db.domains.GoodsType newGoodsType,
			boolean update) {
		goodsType = newGoodsType;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (goodsType != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}

	public void list(){
			DefaultTableModel tm=(DefaultTableModel) table.getModel();
			tm.setRowCount(0);
			Home home=new Home();
			@SuppressWarnings("unchecked")
			List<GoodsType> list=home.list(Home.LIST_GOODTYPES,null);
			int i=1;
			for(GoodsType g:list){
				tm.addRow(new String[]{i+"",g.getName(),g.getDescription()});
				i++;
			}
			home.closeEntityManager();
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}
	
}
